package com.cs.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cs.constant.RoleConstant;
import com.cs.domain.SysRole;
import com.cs.mapper.SysRoleMapper;
import com.cs.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParseException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 查询所有的角色
     *
     * @return
     */
    @Override
    public List<SysRole> findListRole() {
        //先查询缓存
        String roleJson = redisTemplate.opsForValue().get(RoleConstant.ROLE_PREFIX);
        List<SysRole> sysRoles=null;
        if(StringUtils.hasText(roleJson)){
            sysRoles = JSON.parseArray(roleJson, SysRole.class);
        }
        sysRoles = sysRoleMapper.selectList(null);
        if(!CollectionUtils.isEmpty(sysRoles)){
             roleJson = JSON.toJSONString(sysRoles);
            //存入Redis中
            redisTemplate.opsForValue().set(RoleConstant.ROLE_PREFIX,roleJson);
        }
        return sysRoles;
    }
}
